﻿using Kingdee.BOS.Core.Bill.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Util;
using LM.YF.K3Cloud.Plugin.Models;
using System;
using System.ComponentModel;
using System.Linq;

namespace LM.YF.K3Cloud.Plugin
{
    /// <summary>
    /// 应付单表单插件
    /// <author>鹿奔</author>
    /// <date>240117</date>
    /// </summary>
    [Description("应付单表单插件"), HotUpdate]
    public class PayableOrderBillPlugin : AbstractBillPlugIn
    {
        #region 需要用到的字段

        /// <summary>
        /// 费用类型
        /// </summary>
        internal readonly ElementKeySchema F_fee_type = new ElementKeySchema
        {
            DbKey = "F_FEE_TYPE",
            FormKey = "F_fee_type",
            OrmKey = "F_fee_type"
        };

        /// <summary>
        /// 基本运费
        /// </summary>
        internal readonly ElementKeySchema F_base_freight_fee = new ElementKeySchema
        {
            DbKey = "F_BASE_FREIGHT_FEE",
            FormKey = "F_base_freight_fee",
            OrmKey = "F_base_freight_fee"
        };

        /// <summary>
        /// 进仓费
        /// </summary>
        internal readonly ElementKeySchema F_fee_jin_cang = new ElementKeySchema
        {
            DbKey = "F_FEE_JIN_CANG",
            FormKey = "F_fee_jin_cang",
            OrmKey = "F_fee_jin_cang"
        };

        /// <summary>
        /// 提货费
        /// </summary>
        internal readonly ElementKeySchema F_fee_ti_huo = new ElementKeySchema
        {
            DbKey = "F_FEE_TI_HUO",
            FormKey = "F_fee_ti_huo",
            OrmKey = "F_fee_ti_huo"
        };

        /// <summary>
        /// 运费计价金额
        /// </summary>
        internal readonly ElementKeySchema F_money = new ElementKeySchema
        {
            DbKey = "F_MONEY",
            FormKey = "F_money",
            OrmKey = "F_money"
        };

        /// <summary>
        /// 重量单位
        /// </summary>
        internal readonly ElementKeySchema F_weight_unit = new ElementKeySchema
        {
            DbKey = "F_WEIGHT_UNIT",
            FormKey = "F_weight_unit",
            OrmKey = "F_weight_unit"
        };

        /// <summary>
        /// 交货数量
        /// </summary>
        internal readonly ElementKeySchema F_deliver_qty = new ElementKeySchema
        {
            DbKey = "F_DELIVER_QTY",
            FormKey = "F_deliver_qty",
            OrmKey = "F_deliver_qty"
        };

        /// <summary>
        /// 运输区域
        /// </summary>
        internal readonly ElementKeySchema F_transport_area = new ElementKeySchema
        {
            DbKey = "F_TRANSPORT_AREA",
            FormKey = "F_transport_area",
            OrmKey = "F_transport_area"
        };

        /// <summary>
        /// 物料重量
        /// </summary>
        internal readonly ElementKeySchema F_material_weight = new ElementKeySchema
        {
            DbKey = "F_MATERIAL_WEIGHT",
            FormKey = "F_material_weight",
            OrmKey = "F_material_weight"
        };

        /// <summary>
        /// 其它费用
        /// </summary>
        internal readonly ElementKeySchema F_fee_other = new ElementKeySchema
        {
            DbKey = "F_FEE_OTHER",
            FormKey = "F_fee_other",
            OrmKey = "F_fee_other"
        };

        /// <summary>
        /// 提货费
        /// </summary>
        internal readonly ElementKeySchema F_fee_ti_huo2 = new ElementKeySchema
        {
            DbKey = "F_FEE_TI_HUO2",
            FormKey = "F_fee_ti_huo2",
            OrmKey = "F_fee_ti_huo2"
        };

        /// <summary>
        /// 进仓费
        /// </summary>
        internal readonly ElementKeySchema F_fee_jin_cang2 = new ElementKeySchema
        {
            DbKey = "F_FEE_JIN_CANG2",
            FormKey = "F_fee_jin_cang2",
            OrmKey = "F_fee_jin_cang2"
        };

        /// <summary>
        /// 基本运费
        /// </summary>
        internal readonly ElementKeySchema F_base_freight_fee2 = new ElementKeySchema
        {
            DbKey = "F_BASE_FREIGHT_FEE2",
            FormKey = "F_base_freight_fee2",
            OrmKey = "F_base_freight_fee2"
        };

        /// <summary>
        /// 通知单号
        /// </summary>
        internal readonly ElementKeySchema F_number_fhtz = new ElementKeySchema
        {
            DbKey = "F_NUMBER_FHTZ",
            FormKey = "F_number_fhtz",
            OrmKey = "F_number_fhtz"
        };

        /// <summary>
        /// 成本中心
        /// </summary>
        internal readonly ElementKeySchema F_cost_center = new ElementKeySchema
        {
            DbKey = "F_COST_CENTER",
            FormKey = "F_cost_center",
            OrmKey = "F_cost_center"
        };

        /// <summary>
        /// 交货单位
        /// </summary>
        internal readonly ElementKeySchema F_deliver_unit = new ElementKeySchema
        {
            DbKey = "F_DELIVER_UNIT",
            FormKey = "F_deliver_unit",
            OrmKey = "F_deliver_unit"
        };

        /// <summary>
        /// 源单类型（明细）
        /// </summary>
        internal readonly ElementKeySchema FSOURCETYPE = new ElementKeySchema
        {
            DbKey = "FSOURCETYPE",
            FormKey = "FSOURCETYPE",
            OrmKey = "FSOURCETYPE"
        };

        /// <summary>
        /// 单据体
        /// </summary>
        internal readonly ElementKeySchema FEntityDetail = new ElementKeySchema
        {
            DbKey = "T_AP_PAYABLEENTRY",
            FormKey = "FEntityDetail",
            OrmKey = "AP_PAYABLEENTRY"
        };

        #endregion

        public override void AfterBindData(EventArgs e)
        {
            base.AfterBindData(e);

            #region 根据源单类型显示或隐藏字段

            string[] instableElements = new string[]
            {
                F_fee_type.FormKey,
                F_base_freight_fee.FormKey,
                F_fee_jin_cang.FormKey,
                F_fee_ti_huo.FormKey,
                F_money.FormKey,
                F_weight_unit.FormKey,
                F_deliver_qty.FormKey,
                F_transport_area.FormKey,
                F_material_weight.FormKey,
                F_fee_other.FormKey,
                F_fee_ti_huo2.FormKey,
                F_fee_jin_cang2.FormKey,
                F_base_freight_fee2.FormKey,
                F_number_fhtz.FormKey,
                F_cost_center.FormKey,
                F_deliver_unit.FormKey
            };
            HideElements(instableElements);

            var rowCount = Model.GetEntryRowCount(FEntityDetail.FormKey);
            if (rowCount == 0)
            {
                return;
            }

            var sourceType = Model.GetValue(FSOURCETYPE.FormKey, 0)?.ToString();
            if ("RDLV_DiliveOrder".Equals(sourceType))// 货运单
            {
                ShowElements(instableElements);
            }

            #endregion
        }

        public override void DataChanged(DataChangedEventArgs e)
        {
            base.DataChanged(e);

            #region 汇总提货费、基本运费、进仓费

            if (F_fee_ti_huo2.FormKey.Equals(e.Field.Key))
            {
                var entity = Model.BusinessInfo.GetEntity(FEntityDetail.FormKey);
                var entityDataCollection = Model.GetEntityDataObject(entity);
                var sum = entityDataCollection.Sum(i => Convert.ToSingle(i[F_fee_ti_huo2.OrmKey]));
                Model.SetValue(F_fee_ti_huo.FormKey, sum);
            }
            else if (F_base_freight_fee2.FormKey.Equals(e.Field.Key))
            {
                var entity = Model.BusinessInfo.GetEntity(FEntityDetail.FormKey);
                var entityDataCollection = Model.GetEntityDataObject(entity);
                var sum = entityDataCollection.Sum(i => Convert.ToSingle(i[F_base_freight_fee2.OrmKey]));
                Model.SetValue(F_base_freight_fee.FormKey, sum);
            }
            else if (F_fee_jin_cang2.FormKey.Equals(e.Field.Key))
            {
                var entity = Model.BusinessInfo.GetEntity(FEntityDetail.FormKey);
                var entityDataCollection = Model.GetEntityDataObject(entity);
                var sum = entityDataCollection.Sum(i => Convert.ToSingle(i[F_fee_jin_cang2.OrmKey]));
                Model.SetValue(F_fee_jin_cang.FormKey, sum);
            }

            #endregion
        }

        private void HideElements(string[] elements)
        {
            foreach (var element in elements)
            {
                View.GetControl(element).Visible = false;
            }
        }

        private void ShowElements(string[] elements)
        {
            foreach (var element in elements)
            {
                View.GetControl(element).Visible = true;
            }
        }
    }
}
